package com.intellagric.service.traceability.Impl.trace_record;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceRecordSaleMapper;
import com.intellagric.module.traceability.RecordMachiningVo;
import com.intellagric.pojo.TraceRecordSale;
import com.intellagric.pojo.TraceRecordSaleExample;
import com.intellagric.service.traceability.trace_record.SalerecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/1/12
 * @DESC： 销售记录管理服务
 */
@Service
public class SalerecordServiceImpl implements SalerecordService {

    @Autowired
    private TraceRecordSaleMapper recordSaleMapper;

    /**
     * 添加销售记录
     * @param recordSale
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addSalerecord(TraceRecordSale recordSale) {
        if (recordSaleMapper.insertSelective(recordSale) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除销售记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteISalerecord(String id) {
        if (recordSaleMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改销售记录
     * @param recordSale
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editSalerecord(TraceRecordSale recordSale) {
        if (recordSaleMapper.updateByPrimaryKeySelective(recordSale) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询销售记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public TraceRecordSale getSalerecordById(String id) {
        return recordSaleMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询销售记录列表
     * @return ResponseMessage
     */
    @Override
    public LayuiDataGridResult getSalerecordList() {
        TraceRecordSaleExample recordSaleExample = new TraceRecordSaleExample();
        recordSaleExample.setOrderByClause("create_date desc");
        recordSaleExample.createCriteria().andIdIsNotNull();
        List<TraceRecordSale> recordSaleList = recordSaleMapper.selectByExample(recordSaleExample);
        return LayuiDataGridResult.success().add(recordSaleList,recordSaleList.size());
    }

    /**
     * 分页查询销售记录列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSalerecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceRecordSaleExample recordSaleExample = new TraceRecordSaleExample();
        recordSaleExample.setOrderByClause("create_date desc");
        recordSaleExample.createCriteria().andIdIsNotNull();
        List<TraceRecordSale> recordSaleList = recordSaleMapper.selectByExample(recordSaleExample);
        PageInfo<TraceRecordSale> pageInfo = new PageInfo<>(recordSaleList);
        return LayuiDataGridResult.success().add(recordSaleList,pageInfo.getTotal());
    }

    /**
     * 多条件分页查询
     * @param page
     * @param rows
     * @param recordMachiningVo
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSalerecordPageByVo(int page, int rows, RecordMachiningVo recordMachiningVo) {
        return null;
    }
}
